package com.example.presentshopping.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bigkoo.convenientbanner.ConvenientBanner
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.IamgeViewBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.net.observer.TaskCallback
import com.example.presentshopping.ui.activity.*
import com.example.presentshopping.ui.custom.NoDoubleClickListener
import com.example.presentshopping.ui.fragment.homefrgme.Recommenframen
import com.example.presentshopping.utils.tool.GsonUtils
import com.example.presentshopping.utils.tool.IntentJumpUtils
import com.example.presentshopping.utils.tool.LogUtils
import com.google.android.material.tabs.TabLayout
import wellijohn.org.scrollviewwithstickheader.ScrollViewWithStickHeader
import java.util.*

class HomeFragment : BaseMvpFragment() {

    val str = arrayOf("爆品推荐", "新品首发", "热销榜单")
    var page = 1
    var type = 1
    private var queryInfoPresenter: PublicPresenter? = null
    var cb_banner: ConvenientBanner<IamgeViewBean>? = null
    var mTabLayout: TabLayout? = null
    var viewPager: ViewPager? = null
    private var pageViews: List<Fragment>? = null
    var mStickScrollView: ScrollViewWithStickHeader? = null
    var mLLStickList: LinearLayout? = null
    override fun initView(view: View?, savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, true, "加载中")
        queryInfoPresenter?.attachView(this)
        mStickScrollView = view?.findViewById(R.id.stick_scroll_view)
        mLLStickList = view?.findViewById(R.id.ll_stick_list)
        mStickScrollView?.setContentView(mLLStickList)
        cb_banner = view?.findViewById<ConvenientBanner<IamgeViewBean>>(R.id.cbanner_img)
        view?.findViewById<ImageView>(R.id.iv_pintun)?.setOnClickListener {
            context?.startActivity(
                Intent(context, SpellgroupActivity().javaClass))
        }
        view?.findViewById<LinearLayout>(R.id.sec_category0)?.setOnClickListener {
            startActivity(
                Intent(context, ExperienceActivity().javaClass))
        }
        view?.findViewById<LinearLayout>(R.id.sec_category1)?.setOnClickListener(OnCancelListener)
        view?.findViewById<LinearLayout>(R.id.sec_category2)?.setOnClickListener(OnCancelListener)
        view?.findViewById<LinearLayout>(R.id.sec_category3)?.setOnClickListener(OnCancelListener)
        view?.findViewById<LinearLayout>(R.id.sec_category4)?.setOnClickListener {
            context?.startActivity(
                Intent(context, SpellgroupActivity().javaClass))
        }
        view?.findViewById<LinearLayout>(R.id.sec_category5)?.setOnClickListener(OnCancelListener)
        view?.findViewById<LinearLayout>(R.id.sec_category6)?.setOnClickListener(OnCancelListener)
        view?.findViewById<LinearLayout>(R.id.sec_category7)?.setOnClickListener(OnCancelListener)
        view?.findViewById<ImageView>(R.id.sec_category8)?.setOnClickListener(OnCancelListener)
        view?.findViewById<ImageView>(R.id.sec_category9)?.setOnClickListener(OnCancelListener)
        mTabLayout = view?.findViewById(R.id.id_stickynavlayout_indicator)
        viewPager = view?.findViewById(R.id.id_stickynavlayout_viewpager)
        getlabem()
        pageViews = ArrayList<Fragment>()
        (pageViews as ArrayList<Fragment>).add(Recommenframen("1")) //基本信息
        (pageViews as ArrayList<Fragment>).add(Recommenframen("2")) //股东信息
        (pageViews as ArrayList<Fragment>).add(Recommenframen("3")) //主要人员

        mTabLayout?.setupWithViewPager(viewPager)
        viewPager!!.adapter = GuidePageAdapter(childFragmentManager,
            pageViews as ArrayList<Fragment>)
    }
    val OnCancelListener = object : NoDoubleClickListener() {
        override fun onNoDoubleClick(v: View?) {
            if (IntentJumpUtils.isLogin()) {
                when (v?.id) {
                    R.id.sec_category1 -> {
                        val iniet = Intent(context, LuckydrawActivity().javaClass)
                        iniet.putExtra("Activity", "Turntable")
                        startActivity(iniet)
                    }
                    R.id.sec_category2 -> {
                        val inito = Intent(context, SignInActivity().javaClass)
                        startActivity(inito)
                    }
                    R.id.sec_category3 -> {
                        val iniet = Intent(context, InformationActivity().javaClass)
                        iniet.putExtra("Activity", "message")
                        startActivity(iniet)
                    }

                    R.id.sec_category5 -> {
                        startActivity(Intent(context, ComesQCasActivity().javaClass))
                    }
                    R.id.sec_category6 -> showToast("功能暂未开放")
                    R.id.sec_category7 -> {
                        //排行榜
//            startActivity(
//                Intent(context, SeniorityActivity().javaClass)
//            )
                        showToast("功能暂未开放")
                    }
                    R.id.sec_category8 -> showToast("功能暂未开放")
                    R.id.sec_category9 -> showToast("功能暂未开放")

                }
            } else {
                // 登录
                //    queryInfoPresenter?.showAlertDialog(MyApplication.getContext(), "")
                IntentJumpUtils.goToLogin(context, "")
            }


        }
    }
    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {
    }

    override fun onSuccess(data: String?) {

    }

    override val layoutId: Int
        get() = R.layout.home_fragment

    //轮播图
    fun getlabem() {
        queryInfoPresenter?.netWorkRequestPost(
            HttpURL.Req_ggetCarousel,
            HttpRequest.getGoodsList(1, 1, 10),
            object : TaskCallback {
                override fun onSuccess(data: String?) {
                    LogUtils.p("onSuccess-----", data)
                    val beanbr = GsonUtils.getGson().fromJson(data, IamgeViewBean().javaClass)
                    if (beanbr.data?.list!!.isEmpty()) {
                    } else {
                        cb_banner?.setPageIndicator(
                            intArrayOf(
                                R.mipmap.xiang_huise,
                                R.mipmap.xing_bai
                            )
                        )
                        cb_banner?.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                        IntentJumpUtils.setPase(cb_banner, beanbr?.data?.list as List<Any>?)
                        cb_banner?.startTurning(3000)
                    }
                }

                override fun onFailure(
                    e: Throwable?,
                    code: Int,
                    msg: String?,
                    isNetWorkError: Boolean
                ) {


                }

            })
    }


    // 指引页面数据适配器
    internal class GuidePageAdapter : FragmentPagerAdapter {
        val str = arrayOf("爆品推荐", "新品首发", "热销榜单")
        var list: List<Fragment>
        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            // 不会重复加载
//            super.destroyItem(container, position, object);
        }

        constructor(fm: FragmentManager, list: List<Fragment>) : super(fm) {
            this.list = list
        }

        constructor(fm: FragmentManager, behavior: Int, list: ArrayList<Fragment>) : super(fm,
            behavior) {
            this.list = list
        }

        override fun getItem(position: Int): Fragment {
            return list[position]
        }

        override fun getCount(): Int {
            return list.size
        }

        override fun getPageTitle(position: Int): CharSequence? { //添加标题Tab
            return str[position]
        }
    }

}